package com.dwj.auto.comment.common.enums;

import com.dwj.auto.comment.common.wechat.handler.*;
import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName WxTextCommand
 * @Description 文本消息命令
 * @Author dwjian
 * @Date 2020/12/26 18:57
 */
public enum WxTextCommand {
    INSTALL_PARAM("安装参数", WxTextCommandType.NORMAL,"idea破解安装参数", InstallParamHandler.class),
    REGISTER("注册", WxTextCommandType.NORMAL,"注册自动评语系统账号", RegisterHandler.class),
    RESET_PASSWORD("重置密码", WxTextCommandType.FUZZY,"重置密码", ResetPasswordHandler.class),
    PLUGIN_PASSWORD("插件密码", WxTextCommandType.FUZZY,"插件密码", PlugInPasswordHandler.class),
    QUANTIFY_TRADING("量化交易", WxTextCommandType.FUZZY,"量化交易", QuantitativeTradingHandler.class);

    WxTextCommand(String command, WxTextCommandType type, String desc, Class<? extends WxMessageHandler> handler) {
        this.command = command;
        this.type = type;
        this.desc = desc;
        this.handler = handler;
    }

    private String command;
    private WxTextCommandType type;
    private String desc;
    private Class<? extends WxMessageHandler> handler;

    public static WxTextCommand match(String command) {
        if(StringUtils.isNotEmpty(command)){
            for (WxTextCommand textCommand : WxTextCommand.values()) {
                //普通匹配
                if(WxTextCommandType.NORMAL.equals(textCommand.getType())){
                    if(command.equals(textCommand.getCommand())){
                        return textCommand;
                    }
                }
                //模糊匹配
                if(WxTextCommandType.FUZZY.equals(textCommand.getType())){
                    if(command.contains(textCommand.getCommand())){
                        return textCommand;
                    }
                }
                //正则匹配
                if(WxTextCommandType.REGEX.equals(textCommand.getType())){
                    Pattern pattern = Pattern.compile(textCommand.getCommand());
                    Matcher matcher = pattern.matcher(command);
                    if(matcher.find()){
                        return textCommand;
                    }
                }
            }
        }
        return null;
    }

    public String getCommand() {
        return command;
    }

    public void setCommand(String command) {
        this.command = command;
    }

    public WxTextCommandType getType() {
        return type;
    }

    public void setType(WxTextCommandType type) {
        this.type = type;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public Class<? extends WxMessageHandler> getHandler() {
        return handler;
    }

    public void setHandler(Class<? extends WxMessageHandler> handler) {
        this.handler = handler;
    }
}
